package richbar.com.richbar.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.base.BaseResult;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.base.S;
import richbar.com.richbar.bean.LoginBean;
import richbar.com.richbar.bean.MerchantEntryResult;
import richbar.com.richbar.bean.MerchantResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.PhotoUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.dialog.TipDialog;

/**
 * Created by QiYang on 2018/10/29.
 * 商家入住
 */

public class MerchantEntryAc extends BaseActivity {
    @BindView(R.id.company_name)
    EditText companyName;
    @BindView(R.id.tel)
    EditText tel;
    @BindView(R.id.legal_person)
    EditText legalPerson;
    @BindView(R.id.id_card_iv)
    ImageView idCardIv;
    @BindView(R.id.license_iv)
    ImageView licenseIv;
    @BindView(R.id.bank_account)
    EditText bankAccount;
    @BindView(R.id.opening_permission_iv)
    ImageView openingPermissionIv;
    @BindView(R.id.trademark_license_iv)
    ImageView trademarkLicenseIv;
    @BindView(R.id.bank)
    EditText bank;
    @BindView(R.id.submit)
    TextView submit;
    private int thisPic = -1;
    private File file0;
    private File file1;
    private File file2;
    private File file3;
    private LoadingDialog loadingDialog;
    private TitleHelp titleHelp;
    private boolean isSubmitClick = false;

    @Override
    public int initLayout() {
        return R.layout.ac_merchan_entry;
    }

    @Override
    public void initTitle() {
        titleHelp = new TitleHelp(this);
        titleHelp.setTitle("商家入住");
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this,0);
    }

    @Override
    public void initData() {
        status();
    }

    private void status() {
        NetWorkUtil.sellerStatus(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final MerchantResult result = MyGson.getInstance().fromJson(data, MerchantResult.class);
                if (checkIsOk(result)){
                    status(result);
                }
            }
        });
    }

    private void status(MerchantResult result) {

        if (result.getData().getStatus() == 0){
            isSubmitClick = true;
        }else {
            MerchantResult.DataEntity bean = result.getData();
            companyName.setFocusable(false);
            tel.setFocusable(false);
            legalPerson.setFocusable(false);
            bankAccount.setFocusable(false);
            bank.setFocusable(false);
            idCardIv.setOnClickListener(null);
            licenseIv.setOnClickListener(null);
            openingPermissionIv.setOnClickListener(null);
            trademarkLicenseIv.setOnClickListener(null);
            companyName.setText(bean.getCname());
            tel.setText(bean.getTel());
            legalPerson.setText(bean.getFname());
            bankAccount.setText(bean.getBanknum());
            bank.setText(bean.getBank());
            GlideUtil.loadImage(this,NetWorkUtil.URL + bean.getFcard(),idCardIv);
            GlideUtil.loadImage(this,NetWorkUtil.URL + bean.getLicense(),licenseIv);
            GlideUtil.loadImage(this,NetWorkUtil.URL + bean.getBankpermit(),openingPermissionIv);
            GlideUtil.loadImage(this,NetWorkUtil.URL + bean.getBrand(),trademarkLicenseIv);
            switch (result.getData().getStatus()){
                case 1:
                    submit.setText("待审核");
                    break;
                case 2:
                    submit.setText("已通过");
                    break;
                case 3:
                    submit.setText("已拒绝");
                    break;
            }
        }
    }


    @OnClick({R.id.id_card_iv, R.id.license_iv, R.id.opening_permission_iv, R.id.trademark_license_iv, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_card_iv:
                PhotoUtil.openPhotoSingle(this);
                thisPic = 0;
                break;
            case R.id.license_iv:
                PhotoUtil.openPhotoSingle(this);
                thisPic = 1;
                break;
            case R.id.opening_permission_iv:
                PhotoUtil.openPhotoSingle(this);
                thisPic = 2;
                break;
            case R.id.trademark_license_iv:
                PhotoUtil.openPhotoSingle(this);
                thisPic = 3;
                break;
            case R.id.submit:
                if (isSubmitClick) {
                    submit();
                }
                break;
        }
    }

    private void submit() {
        String companyNameTv = StringUtil.getStr(companyName);
        String telTv= StringUtil.getStr(tel);
        String legalPersonTv= StringUtil.getStr(legalPerson);
        String bankAccountTv = StringUtil.getStr(bankAccount);
        String bankTv = StringUtil.getStr(bank);
        if (null != file0 && null != file1 && null != file2 && null != file3 && !StringUtil.isEmpty(
                companyNameTv, telTv, legalPersonTv, bankAccountTv, bankTv)){
            submit2Net(companyNameTv,telTv,legalPersonTv,bankAccountTv,bankTv);
        }else {
            ToastUtil.showToast(this,getString(R.string.plz_all));
        }
    }

    private void submit2Net(String companyNameTv, String telTv, String legalPersonTv, String bankAccountTv, String bankTv) {
        loadingDialog.show();
        NetWorkUtil.sellerApply(BaseApplication.uid, companyNameTv, telTv, legalPersonTv, file0, file1,
                bankAccountTv, bankTv, file2, file3, Util.md5(), new HttpUtils.HttpCallback() {
                    @Override
                    public void onSuccess(String data) {
                        loadingDialog.dismiss();
                        final MerchantEntryResult result = MyGson.getInstance().fromJson(data, MerchantEntryResult.class);
                        if (checkIsOk(result)){
                            TipDialog.show(MerchantEntryAc.this, "工作人员正在审核中，\n请耐心等待，\n3个工作日内给与答案", new TipDialog.OnListener() {
                                @Override
                                public void onBesure() {
                                    finish();
                                }
                            });
                        }
                    }
                });
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> list = PictureSelector.obtainMultipleResult(data);
            if (null == list || list.size() == 0){
                return;
            }
            String path = list.get(0).getPath();
            switch (thisPic) {
                case 0:
                    GlideUtil.loadImage(this, path, idCardIv);
                    file0 = new File(path);
                    break;
                case 1:
                    GlideUtil.loadImage(this, path, licenseIv);
                    file1 = new File(path);
                    break;
                case 2:
                    GlideUtil.loadImage(this, path, openingPermissionIv);
                    file2 = new File(path);
                    break;
                case 3:
                    GlideUtil.loadImage(this, path, trademarkLicenseIv);
                    file3 = new File(path);
                    break;
            }
        }
    }

}
